home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / asmutil / disasm.zip / ASMTUTR3.DOC < prev    next >
Text File  |  1988-06-03  |  19KB  |  338 lines

  1.   PROGPREF SEGMENT   AT 0      ;Really a DSECT mapping the program prefix
  2.            ORG   PROGPREF+6
  3.   MEMSIZE  DW   ?              ;Size of available memory
  4.   PROGPREF ENDS
  5. Really, no matter whether the AT value represents truth or fiction, it is
  6. your responsibility, not the assembler's, to get set up a segment register
  7. so that you can really reach the storage in question.   So, you can't say
  8.          MOV  AL,EQUIP
  9. unless you first say something like
  10.          MOV  AX,BIOSAREA   ;BIOSAREA becomes a symbol with value 40H
  11.          MOV  ES,AX
  12.          ASSUME ES:BIOSAREA
  13. Enough about SEGMENT.  The END statement is simple.  It goes at the end of
  14. every assembly.  When you are assembling a subroutine, you just say
  15.          END
  16. but when you are assembling the main routine of a program you say
  17.         END label
  18. where 'label' is the place where execution is to begin.
  19. Another pseudo-op illustrated in the program is ASSUME.  ASSUME is like the
  20. USING statement in 370 assembler.  However, ASSUME can ONLY refer to seg-
  21. ment registers.  The assembler uses ASSUME information to decide whether to
  22. assemble segment override prefixes and to check that the data you are try-
  23. ing to access is really accessible.  In this case, we can reassure the
  24. assembler that both the CS and DS registers will address the section called
  25. HELLO at execution time.  Actually, the SS and ES registers will too, but
  26. the assembler never needs to make use of this information.
  27. I guess I have explained everything in the program except that ORG
  28. pseudo-op.  ORG means the same thing as it does in many assembly languages.
  29. It tells the assembler to move its location counter to some particular
  30. address.  In this case, we have asked the assembler to start assembling
  31. code hex 100 bytes from the start of the section called HELLO instead of at
  32. the very beginning.  This simply reflects the way COM programs are loaded.
  33. When a COM program is loaded by the system, the system sets up all four
  34. segment registers to address the same 64K of storage.  The first 100 hex
  35. bytes of that storage contains what is called the program prefix; this area
  36. is described in appendix E of the DOS manual.  Your COM program physically
  37. begins after this.  Execution begins with the first physical byte of your
  38. program; that is why the JMP instruction is there.
  39. Wait a minute, you say, why the JMP instruction at all?  Why not put the
  40. data at the end?  Well, in a simple program like this I probably could have
  41. gotten away with that.  However, I have the habit of putting data first and
  42. would encourage you to do the same because of the way the assembler has of
  43. assembling different instructions depending on the nature of the operand.
  44. IBM PC Assembly Language Tutorial                                        19
  45.  
  46.  
  47. Unfortunately, sometimes the different choices of instruction which can
  48. assemble from a single opcode have different lengths.  If the assembler has
  49. already seen the data when it gets to the instructions it has a good chance
  50. of reserving the right number of bytes on the first pass.  If the data is
  51. at the end, the assembler may not have enough information on the first pass
  52. to reserve the right number of bytes for the instruction.  Sometimes the
  53. assembler will complain about this, something like "Forward reference is
  54. illegal" but at other times, it will make some default assumption.  On the
  55. second pass, if the assumption turned out to be wrong, it will report what
  56. is called a "Phase error," a very nasty error to track down.  So get in the
  57. habit of putting data and equated symbols ahead of code.
  58. OK.  Maybe you understand the program now.  Let's walk through the steps
  59. involved in making it into a real COM file.
  60. 1.  The file should be created with the name HELLO.ASM (actually the name
  61.     is arbitrary but the extension .ASM is conventional and useful)
  62. 2.
  63.       ASM   HELLO,,;
  64.     (this is just one example of invoking the assembler; it uses the small
  65.     assembler ASM, it produces an object file and a listing file with the
  66.     same name as the source file.  I am not going exhaustively into how to
  67.     invoke the assembler, which the manual goes into pretty well.  I guess
  68.     this is the first time I mentioned that there are really two
  69.     assemblers; the small assembler ASM will run in a 64K machine and
  70.     doesn't support macros.  I used to use it all the time; now that I have
  71.     a bigger machine and a lot of macro libraries I use the full function
  72.     assembler MASM.  You get both when you buy the package).
  73. 3.  If you issue DIR at this point, you will discover that you have
  74.     acquired HELLO.OBJ (the object code resulting from the assembly) and
  75.     HELLO.LST (a listing file).  I guess I can digress for a second here
  76.     concerning the listing file.  It contains TAB characters.  I have found
  77.     there are two good ways to get it printed and one bad way.  The bad way
  78.     is to use LPT1: as the direct target of the listing file or to try
  79.     copying the LST file to LPT1 without first setting the tabs on the
  80.     printer.  The two good ways are to either
  81.     a.  direct it to the console and activate the printer with CTRL-PRTSC.
  82.         In this case, DOS will expand the tabs for you.
  83.     b.  direct to LPT1: but first send the right escape sequence to LPT1 to
  84.         set the tabs every eight columns.  I have found that on some early
  85.         serial numbers of the IBM PC printer, tabs don't work quite right,
  86.         which forces you to the first option.
  87. 4.
  88.           LINK  HELLO;
  89.     (again, there are lots of linker options but this is the simplest.  It
  90.     takes HELLO.OBJ and makes HELLO.EXE).  HELLO.EXE?  I thought we were
  91. IBM PC Assembly Language Tutorial                                        20
  92.  
  93.  
  94.     making a COM program, not an EXE program.  Right.  HELLO.EXE isn't
  95.     really executable; its just that the linker doesn't know about COM pro-
  96.     grams.  That requires another utility.  You don't have this utility if
  97.     you are using DOS 1.0; you have it if you are using DOS 1.1 or DOS 2.0.
  98.     Oh, by the way, the linker will warn you that you have no stack
  99.     segment.  Don't worry about it.
  100. 5.
  101.           EXE2BIN  HELLO HELLO.COM
  102.     This is the final step.  It produces the actual program you will exe-
  103.     cute.  Note that you have to spell out HELLO.COM; for a nominally
  104.     rational but actually perverse reason, EXE2BIN uses the default exten-
  105.     sion BIN instead of COM for its output file.  At this point, you might
  106.     want to erase HELLO.EXE; it looks a lot more useful than it is.
  107.     Chances are you won't need to recreate HELLO.COM unless you change the
  108.     source and then you are going to have to redo the whole thing.
  109. 6.
  110.           HELLO
  111.     You type hello, that invokes the program, it says
  112.           HELLO YOURSELF!!!
  113.     (oops, what did I do wrong....?)
  114.  
  115. What about subroutines?
  116. _______________________
  117. What about subroutines?
  118. What about subroutines?
  119. What about subroutines?
  120. I started with a simple COM program because I actually think they are easi-
  121. er to create than subroutines to be called from high level languages, but
  122. maybe its really the latter you are interested in.  Here, I think you
  123. should get comfortable with the assembler FIRST with little exercises like
  124. the one above and also another one which I will finish up with.
  125. Next you are ready to look at the interface information for your particular
  126. language.  You usually find this in some sort of an appendix.  For example,
  127. the BASIC manual has Appendix C on Machine Language Subroutines.  The
  128. PASCAL manual buries the information a little more deeply:  the interface
  129. to a separately compiled routine can be found in the Chapter on Procedures
  130. and Functions, in a subsection called Internal Calling Conventions.
  131. Each language is slightly different, but here are what I think are some
  132. common issues in subroutine construction.
  133. 1.  NEAR versus FAR?  Most of the time, your language will probably call
  134.     your assembler routine as a FAR routine.  In this case, you need to
  135.     make sure the assembler will generate the right kind of return.  You do
  136.     this with a P